<?php

namespace App\Admin\Actions\RadialBar\Home;

use App\Models\SwtOrder;
use Dcat\Admin\Widgets\Metrics\RadialBar;
use Illuminate\Http\Request;

class Orders extends RadialBar
{
    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $this->title('订单统计');
        $this->height(440);
        $this->chartHeight(300);
        $this->chartLabels('订单支付率');
        $this->dropdown([
            '1' => '本日',
            '7' => '近七天',
            '30' => '近一月',
            '365' => '近一年'
        ]);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {

        $count = 0;

        $finish = 0;

        $cancel = 0;

        $warning = 0;

        $finish_rate = 100;

        $where = [

        ];

        switch ($request->get('option')) {
            case '365':
//                $where[] = ['created_at','between', date]
                $count = SwtOrder::whereYear('created_at', date('Y',time()));
            case '30':
                $count = SwtOrder::whereYear('created_at', date('Y',time()))->whereMonth('created_at', date('m',time()));
            case '2':
            case '1':
        }

        // 卡片内容
        $this->withContent($count);
        // 卡片底部
        $this->withFooter($finish, $cancel, $warning);
        // 图表数据
        $this->withChart($finish_rate);
    }

    /**
     * 设置图表数据.
     *
     * @param int $data
     *
     * @return $this
     */
    public function withChart(int $data)
    {
        return $this->chart([
            'series' => [$data],
        ]);
    }

    /**
     * 卡片内容
     *
     * @param string $content
     *
     * @return $this
     */
    public function withContent($content)
    {
        return $this->content(
            <<<HTML
<div class="d-flex flex-column flex-wrap text-center">
    <h1 class="font-lg-2 mt-2 mb-0">{$content}</h1>
    <small>总订单数</small>
</div>
HTML
        );
    }

    /**
     * 卡片底部内容.
     *
     * @param string $new
     * @param string $open
     * @param string $response
     *
     * @return $this
     */
    public function withFooter($new, $open, $response)
    {
        return $this->footer(
            <<<HTML
<div class="d-flex justify-content-between p-1" style="padding-top: 0!important;">
    <div class="text-center">
        <p>完结订单</p>
        <span class="font-lg-1">{$new}</span>
    </div>
    <div class="text-center">
        <p>取消订单</p>
        <span class="font-lg-1">{$open}</span>
    </div>
    <div class="text-center">
        <p>异常订单</p>
        <span class="font-lg-1">{$response}</span>
    </div>
</div>
HTML
        );
    }
}
